package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Driver;
import models.Vehicle;
@Entity
@Table(name="violation_record")
public class Violation_record extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@ManyToOne
	public Driver driver; //司机
	@ManyToOne
	public Vehicle vehicle; //车号
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date violation_date; //违章日期
	
	public String violation_receipe; //违规违法处罚单据
	
	public String violation_content; //违规违法内容
	
	public String violation_place; //违规地点
	
	public String process_type; //处理形式
	public static Model.Finder<Integer, Violation_record> find = new Model.Finder<Integer, Violation_record>(Integer.class, Violation_record.class);

	public static Page<Violation_record> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Violation_record> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setDriver(Driver d) {
		driver= d;
	}
	public Driver getDriver() {
		return driver;
	}
	public void setVehicle(Vehicle d) {
		vehicle= d;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setViolation_date(Date d) {
		violation_date= d;
	}
	public Date getViolation_date() {
		return violation_date;
	}
	public void setViolation_receipe(String d) {
		violation_receipe= d;
	}
	public String getViolation_receipe() {
		return violation_receipe;
	}
	public void setViolation_content(String d) {
		violation_content= d;
	}
	public String getViolation_content() {
		return violation_content;
	}
	public void setViolation_place(String d) {
		violation_place= d;
	}
	public String getViolation_place() {
		return violation_place;
	}
	public void setProcess_type(String d) {
		process_type= d;
	}
	public String getProcess_type() {
		return process_type;
	}
	public static Violation_record findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Violation_record v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
	public static Violation_record findByDriver(Driver d) {
		return find.where().eq("driver", d).findUnique();
	}
	public static String getDriverJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Violation_record v: find.all()) {
			node.put(v.getDriver().toString(), v.getDriver().toString()); 
		}
		return node.toString();
	}
	public static Violation_record findByVehicle(Vehicle d) {
		return find.where().eq("vehicle", d).findUnique();
	}
	public static String getVehicleJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Violation_record v: find.all()) {
			node.put(v.getVehicle().toString(), v.getVehicle().toString()); 
		}
		return node.toString();
	}
	public static Violation_record findByViolation_date(Date d) {
		return find.where().eq("violation_date", d).findUnique();
	}
	public static String getViolation_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Violation_record v: find.all()) {
			node.put(v.getViolation_date().toString(), v.getViolation_date().toString()); 
		}
		return node.toString();
	}
	public static Violation_record findByViolation_receipe(String d) {
		return find.where().eq("violation_receipe", d).findUnique();
	}
	public static String getViolation_receipeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Violation_record v: find.all()) {
			node.put(v.getViolation_receipe().toString(), v.getViolation_receipe().toString()); 
		}
		return node.toString();
	}
	public static Violation_record findByViolation_content(String d) {
		return find.where().eq("violation_content", d).findUnique();
	}
	public static String getViolation_contentJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Violation_record v: find.all()) {
			node.put(v.getViolation_content().toString(), v.getViolation_content().toString()); 
		}
		return node.toString();
	}
	public static Violation_record findByViolation_place(String d) {
		return find.where().eq("violation_place", d).findUnique();
	}
	public static String getViolation_placeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Violation_record v: find.all()) {
			node.put(v.getViolation_place().toString(), v.getViolation_place().toString()); 
		}
		return node.toString();
	}
	public static Violation_record findByProcess_type(String d) {
		return find.where().eq("process_type", d).findUnique();
	}
	public static String getProcess_typeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Violation_record v: find.all()) {
			node.put(v.getProcess_type().toString(), v.getProcess_type().toString()); 
		}
		return node.toString();
	}
}
